package com.baizhi.hospital.controller;

import com.baizhi.hospital.entity.HospLog;
import com.baizhi.hospital.entity.User;
import com.baizhi.hospital.service.HospLogService;
import com.baizhi.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private HospLogService hospLogService;

	@RequestMapping("/login")
	public String login(String username, String password, String code, HttpSession session, Map mapl){
//		判断验证码的正确性
		String validationCode = (String) session.getAttribute("code");
		if(validationCode.equals(code)){
			mapl.put("error","验证码错误");
			return "login";
		}


		User user = userService.selectUserNameAndpassword(username, password);
		if(user != null){
			session.setAttribute("user",user);
//			记录日志
			HospLog hospLog = new HospLog();
			hospLog.setLogId(UUID.randomUUID().toString().replace("-",""));
			hospLog.setLogType(1);
			hospLog.setLogDate(LocalDateTime.now());
			hospLog.setLogOprater(user.getRealName());
			hospLog.setLogContent("用户："+user.getRealName()+"登陆");
			hospLogService.insertLog(hospLog);
			return "hospital";
		}else{
			mapl.put("error","账号或密码错误，请重新输入");
			return "login";
		}

	}
}
